<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Title_FormItem" prop="Entity.Title" :rules="[{ required: true, message: '标题为必填项', trigger: 'blur' }]">
								<template #label>
									<WtmLabelTooltip label="标题" tooltip="待办事项的标题" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Title" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_BillCode_FormItem" prop="Entity.BillCode">
								<template #label>
									<WtmLabelTooltip label="单据编码" tooltip="待办事项的单据编码" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.BillCode" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PcRoute_FormItem" prop="Entity.PcRoute">
								<template #label>
									<WtmLabelTooltip label="路由地址" tooltip="处理该待办的页面路由" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.PcRoute" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_BusinessType_FormItem" prop="Entity.BusinessType">
								<template #label>
									<WtmLabelTooltip label="业务类型" tooltip="待办事项的业务类型" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.BusinessType" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ActivityId_FormItem" prop="Entity.ActivityId">
								<template #label>
									<WtmLabelTooltip label="业务ID" tooltip="关联的业务数据ID" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ActivityId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>流程信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WorkflowId_FormItem" prop="Entity.WorkflowId">
								<template #label>
									<WtmLabelTooltip label="流程实例ID" tooltip="工作流实例的唯一标识" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WorkflowId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>组织信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Company_FormItem" prop="Entity.Company">
								<template #label>
									<WtmLabelTooltip label="公司名称" tooltip="待办事项所属公司" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Company" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Department_FormItem" prop="Entity.Department">
								<template #label>
									<WtmLabelTooltip label="部门名称" tooltip="待办事项所属部门" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Department" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>人员信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Submitter_FormItem" prop="Entity.Submitter">
								<template #label>
									<WtmLabelTooltip label="发起人姓名" tooltip="流程发起人的姓名" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Submitter" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Approver_FormItem" prop="Entity.Approver">
								<template #label>
									<WtmLabelTooltip label="处理人" tooltip="处理人" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Approver" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_CreateBy_FormItem" prop="Entity.CreateBy">
								<template #label>
									<WtmLabelTooltip label="创建人" tooltip="创建人" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.CreateBy" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>状态信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ApproveResult_FormItem" prop="Entity.ApproveResult">
								<template #label>
									<WtmLabelTooltip label="办理结果" tooltip="待办事项的处理结果" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ApproveResult" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Status_FormItem" prop="Entity.Status">
								<template #label>
									<WtmLabelTooltip label="办理状态" tooltip="待办事项的处理状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.Status" clearable class="width100">
									<el-option key="待办" value="待办" label="待办"></el-option>
									<el-option key="已办" value="已办" label="已办"></el-option>
									<el-option key="已协同" value="已协同" label="转办"></el-option>
									<el-option key="我的提交" value="我的提交" label="我的提交"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Priority_FormItem" prop="Entity.Priority">
								<template #label>
									<WtmLabelTooltip label="优先级" tooltip="待办事项的优先级" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.Priority" clearable class="width100">
									<el-option key="低" value="低" label="低"></el-option>
									<el-option key="普通" value="普通" label="普通"></el-option>
									<el-option key="高" value="高" label="高"></el-option>
									<el-option key="紧急" value="紧急" label="紧急"></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>时间信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_CreateTime_FormItem" prop="Entity.CreateTime">
								<template #label>
									<WtmLabelTooltip label="创建时间" tooltip="待办事项的创建时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.CreateTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_DueDate_FormItem" prop="Entity.DueDate">
								<template #label>
									<WtmLabelTooltip label="到期时间" tooltip="待办事项的到期处理时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.DueDate" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_CompleteTime_FormItem" prop="Entity.CompleteTime">
								<template #label>
									<WtmLabelTooltip label="完成时间" tooltip="待办事项的完成时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.CompleteTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysTodoCenterApi as api } from '/@/views/_admin/systodocenter/api';
import other from '/@/utils/other';
import { ISysTodoCenterVm } from '/@/views/_admin/systodocenter/api/interface';
import { IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysTodoCenterVm>>({
	vmModel: {
		Entity: {
			ID: null,
			Title: null,
			BillCode: null,
			PcRoute: null,
			MobileRoute: null,
			BusinessId: null,
			BusinessType: null,
			ActivityId: null,
			WorkflowId: null,
			Company: null,
			Department: null,
			Submitter: null,
			Approver: null,
			CreateBy: null,
			ApproveResult: null,
			Status: null,
			Priority: null,
			CreateTime: new Date(),
			DueDate: null,
			CompleteTime: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
});

const handleApiResponse = (response: any, isSubmit = false) => {
	ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
	emit('refresh');
	loading.value = false;
	closeDialog();
};
// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 公共的API请求处理函数
const handleApiRequest = (apiCall: () => Promise<any>, beforeSubmit: () => void, isSubmit = false) => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			beforeSubmit();
			apiCall()
				.then((response: any) => handleApiResponse(response, isSubmit))
				.catch(handleError);
		}
	});
};

// 取消
const onClose = () => {
	closeDialog();
};

// 保存
const onSave = () => {
	handleApiRequest(
		() => api().edit(stateData.vmModel),
		() => {},
		false
	);
};

// 加载选择列表数据
const loadData = async () => {
	// 如果没有传入数据，才从API加载
	if (!ci.attrs['wtmdata']) {
		await Promise.allSettled([
			api()
				.get(stateData.vmModel.Entity.ID ?? '')
				.then((x) => other.setValue(stateData.vmModel, x)),
		]);
	}
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata) {
		// 将传入的数据设置到表单中
		other.setValue(stateData.vmModel.Entity, wtmdata);
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SUBMIT,
			type: 'primary',
			onClick: onSave,
			visible: true,
		},
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
